package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.ExtensionListBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import kotlinx.android.synthetic.main.activity_my_extension.*
import java.util.ArrayList

class MyExtension : BaseAty() {
    private val datas=ArrayList<ExtensionListBean.DataBean>()
    private  lateinit var adapter:BaseQuickAdapter<ExtensionListBean.DataBean,BaseViewHolder>
    override fun getContentId(): Int = R.layout.activity_my_extension

    override fun initView() {

        adapter=object :BaseQuickAdapter<ExtensionListBean.DataBean,BaseViewHolder>(R.layout.my_extension_item,datas){
            override fun convert(helper: BaseViewHolder?, item: ExtensionListBean.DataBean?) {
                Glide.with(mContext).load(item?.avatar).into(helper?.getView(R.id.userIcon))
                helper?.setText(R.id.userName,item?.name)
                helper?.setText(R.id.userNick,item?.user_nickname)
            }
        }
        listView.adapter=adapter
        listView.layoutManager=LinearLayoutManager(this,LinearLayout.VERTICAL,false)

        dispatchData()
    }

    private fun dispatchData() {
        OkGo.post<ExtensionListBean>(Urls.BaseUrl+Urls.EXTENSION_LIST)
                .execute(object :JsonCallBack<ExtensionListBean>(this){
                    override fun onSuccess(response: Response<ExtensionListBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            tv.text="推荐用户数： ${bean?.data.size}"
                            datas.addAll(bean?.data)
                            adapter.notifyLoadMoreToLoading()
                        }
                    }
                })
    }

    override fun initListener() {

        btExit.setOnClickListener {
            finish()
        }
        adapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, MyExtensionDetailAty::class.java)
            intent.putExtra("id",datas[position].id.toString())
            startActivity(intent)
        }
    }

}
